<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <canvas id="canvas"></canvas>
    <script>
      const canvas = document.getElementById("canvas");
      canvas.width = window.innerWidth;
      canvas.height = window.innerHeight;

      const ctx = canvas.getContext("2d");
      ctx.translate(canvas.width / 2, canvas.height);
      ctx.scale(1, -1);

      function drawBranch(v0, len, thick, angle) {
        if (thick < 3.5) {
          ctx.beginPath();
          ctx.arc(...v0, 4, 0, 2 * Math.PI);
          ctx.fillStyle = "#b381b3";
          ctx.fill();
          return;
        }
        ctx.beginPath();
        ctx.moveTo(...v0);
        const v1 = [
          v0[0] + len * Math.cos((angle * Math.PI) / 180),
          v0[1] + len * Math.sin((angle * Math.PI) / 180),
        ];
        ctx.lineTo(...v1);
        ctx.strokeStyle = "#333";
        ctx.lineWidth = thick;
        ctx.stroke();
        drawBranch(v1, len * 0.9, thick * 0.9, angle + Math.random() * 30);
        drawBranch(v1, len * 0.9, thick * 0.9, angle - Math.random() * 30);
      }
      drawBranch([0, 0], 100, 10, 90);
    </script>
  </body>
</html>
